//
//  TransView.m
//  KidReading
//
//  Created by telen on 15/1/4.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TransView.h"
#import "ConstDefine.h"

@implementation TransView

@synthesize isInShowing = isTransformWorking;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
        //
        UIImageView* bgv = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:bgv];
        UIImage* img = [UIImage imageNamed:@"getWord_bg_.png"];
        img = [img stretchableImageWithLeftCapWidth:img.size.width/2 topCapHeight:img.size.height/2];
        bgv.image = img;
        bgview = bgv;
        //
//        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
//        CGFloat pod = 6/ratio_w;
//        frame.size.width -= pod*2;
//        frame.size.height -= pod*2;
//        frame.origin.x += pod;
//        frame.origin.y += pod;
//        UIView* headerView = [[UIView alloc] initWithFrame:frame];
//        [self addSubview:headerView];
//        bgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"getWord_bg_rightcorne.png"]];
//        [headerView addSubview:bgv];
//        bgv.center = CGPointMake(headerView.frame.size.width-bgv.frame.size.width/2, bgv.frame.size.height*0.3);
//        bgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"getWord_bg_leftcorner.png"]];
//        [headerView addSubview:bgv];
//        bgv.center = CGPointMake(bgv.frame.size.width/2, bgv.frame.size.height*0.3);
//        [headerView.layer setCornerRadius:12/ratio_w];
//        [headerView.layer setMasksToBounds:YES];
        //
        //
        CGFloat w = frame.size.width;
        CGFloat h = frame.size.height;
        lineHeight_content = w*0.08;
        //
        CGFloat pod_w = w*0.07;
        CGFloat s_y = 20 / ratio_w;
        frame = CGRectMake(pod_w, s_y, w-pod_w*2.2, h-s_y-pod_w);
        label = [[UILabel alloc] initWithFrame:frame];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:lineHeight_content*0.8];
        [self addSubview:label];
        //
        //判断是否含有i
        if ([ReadBookVOrH_VC currentReadInstance].showInfoDict.count > 0) {
            CGFloat bigger_ratio = ratio_w;
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
                bigger_ratio = ratio_w / 1.3;
            }
            UIButton* showInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [showInfoBtn addTouchScaleTransitions];
            [showInfoBtn setTitle:@"i" forState:UIControlStateNormal];
            [showInfoBtn setTitleColor:[UIColor colorWithHexString:@"#7E8FDA"] forState:UIControlStateNormal];
            showInfoBtn.layer.cornerRadius = 15 / bigger_ratio;
            showInfoBtn.layer.borderColor = [UIColor colorWithHexString:@"#7E8FDA"].CGColor;
            showInfoBtn.layer.borderWidth = 1;
            showInfoBtn.titleLabel.font = [UIFont boldSystemFontOfSize:25 / bigger_ratio];
            showInfoBtn.alpha = 0.6;
            [showInfoBtn addTarget:self action:@selector(tapShowInfo:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:showInfoBtn];
            [showInfoBtn JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
                make.bottom.JRMAS_equalTo(-15 / bigger_ratio);
                make.height.width.JRMAS_equalTo(30 / bigger_ratio);
                make.left.JRMAS_equalTo(pod_w);
            }];
            CGRect bounds = self.bounds;
            bounds.size.height += 40 / bigger_ratio;
            self.bounds = bounds;
            bounds = bgview.bounds;
            bounds.size.height += 40 / bigger_ratio;
            bgview.frame = bounds;
        }
        //
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSelfUndo)];
        [self addGestureRecognizer:tap];
        //
        //创建手势
        UIPanGestureRecognizer *panGR =
        [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragged:)];
        //限定操作的触点数
        [panGR setMaximumNumberOfTouches:1];
        [panGR setMinimumNumberOfTouches:1];
        //将手势添加到draggableObj里
        [self addGestureRecognizer:panGR];
    }
    return self;
}
//
-(void)tapShowInfo:(UIButton*)button{
    NSDictionary* showInfo = [ReadBookVOrH_VC currentReadInstance].showInfoDict;
    [[WaitingShow shareSingleShow] showString:[DataProcessing chooseLanguageFromCN:showInfo[@"transNameZh"] andEN:showInfo[@"transNameEn"]] andAutoHide:YES];
}
//
- (void)setText:(NSString *)str
{
    CGFloat height = label.size.height;
    label.text = str;
    [label resizeToFit];
    CGFloat newHeight = label.size.height;
    newHeight -= height;
    //
    CGRect bounds = self.bounds;
    bounds.size.height += newHeight;
    self.bounds = bounds;
    bounds = bgview.bounds;
    bounds.size.height += newHeight;
    bgview.frame = bounds;
}

- (void)dealloc
{
#if __Debug_is_dealloced_log
    NSLog(@"dealloc: %@",NSStringFromClass([self class]));
#endif
}

- (void)didDragged:(UIPanGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateChanged ||
        sender.state == UIGestureRecognizerStateEnded) {
        //注意，这里取得的参照坐标系是该对象的上层View的坐标。
        CGPoint offset = [sender translationInView:self.superview];
        UIView *draggableObj = self;
        //通过计算偏移量来设定draggableObj的新坐标
        CGPoint pt = CGPointMake(draggableObj.center.x + offset.x, draggableObj.center.y + offset.y);
        [draggableObj setCenter:pt];
        //初始化sender中的坐标位置。如果不初始化，移动坐标会一直积累起来。
        [sender setTranslation:CGPointMake(0, 0) inView:self.superview];
        
        //telen 统计
        //if (sender.state == UIGestureRecognizerStateEnded) {
           // [UMSAgent postEvent:@"Toolbar_trans_drag" label:self.bookName];
            //[NSObject msgToClassName:@"UMSAgent" menthed:@"postEvent:label:" refs:@[@"Toolbar_trans_drag",self.bookName] needReturnValue:NO];
        //}
    }
}

- (void)tapSelfUndo{}

- (void)show
{
    if (isTransformWorking) {
        return;
    }
    isTransformWorking = YES;
    self.center = self.pt_unshowFollowView.center;
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.hidden = NO;
        self.center = self.pt_show;
    }completion:^(BOOL finished) {
        isTransformWorking = NO;
        if (_delegate && [_delegate respondsToSelector:@selector(transViewDidShow)]) {
            [_delegate transViewDidShow];
        }
    }];
}

- (void)unShow
{
    if (isTransformWorking) {
        return;
    }
    [self unShow_must];
}

- (void)unShow_must
{
    isTransformWorking = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeScale(0.000001, 0.000001);
        self.hidden = NO;
        self.center = self.pt_unshowFollowView.center;
    }completion:^(BOOL finished) {
        self.hidden = YES;
        isTransformWorking = NO;
        self.pt_unshowFollowView = nil;
        if (_delegate && [_delegate respondsToSelector:@selector(transViewDidUnshow)]) {
            [_delegate transViewDidUnshow];
        }
    }];
}

@end
